var hosturl = "http://www.spider.com.cn/";
var $$ = jQuery;
$$(document).ready(function(){
	var url_remote = "/productmodify.action";
    $.getJSON(url_remote,{
        productType:"second017"
    },function (data){
    	if(null != data){
    		var list = data;
     		var alldata = "<h3>最近加入的商品：</h3><ol>";
     		 for(var i=0;i<list.length;i++){
     			 var arr = list[i];    
                 var objjson = JSON.parse(arr.orderAttributeMap.slice(1,arr.orderAttributeMap.length-1));
                 var pic = objjson.picture;
                 var name = arr.productName;
                 var productid = arr.productId;
                 var quantity =  arr.quantity;
                 //nums = nums + parseInt(quantity);
                 var ptype = arr.productType;
                 var urll = "/"+ptype+"/"+productid+".html";
                 if(name.length>12){
                     name = name.substring(0,12)+"...";
                 }
	     			alldata += "<li id=\"rem" + arr.uuid + "\">";
	     			alldata += "<span class=\"pic\"><a target=\"_blank\" href=\"" + urll + "\"><img src=\"" +pic + "\" width=\"43\" height=\"43\"></a></span>";
	     			alldata += "<dl>";
	     			alldata += "<dt><a target=\"_blank\" href=\"" + urll + "\">" + name + "</a></dt>";
	     			alldata += "</dl>";
	     			alldata += "<span class=\"trlrt\"><strong>&yen;<em>" + arr.price+ "</em></strong><a onclick=\"removeproduct('" + arr.uuid + "','" + productid + "','" + quantity + "')\" href=\"javascript:void(0)\">删除</a></span>";
	     			alldata += "</li>";
	     	   }
	     		alldata += "</ol>";
	     		//<div class="trltotl">
					//<span>购物车里有<span id="_ordercartnum"></span>件商品</span><a target="_blank" href="ordercart.html">查看我的购物车</a>
				//</div>
	     		var datacount = $("#realordercart_num").html();
	     		alldata += "<div class=\"trltotl\">";
	     		alldata += "<span id=\"_ordercartnum\">购物车里有" + datacount + "件商品</span><a target=\"_blank\" href=\"spiderCart_cartList.html\">查看我的购物车</a>";
	     		alldata += "</div>";
	     		$(".trlcont").html(alldata);
	     		//$("#realordercart_num").html(datacount);
     		
		}
    });
});

/**移除购物车**/
function removeproduct(uuid,productid,quantity){
	var map = {};
	map[uuid] = productid;	
	var customerId=$("#customerid").val();
	var allcount = $("#realordercart_num").html();
	if(customerId ==""){
		window.location.href = hosturl + "userlogin.html";
	}
	//向接口发送删除请求
	var datainfo = {
			restful:"delete",
			customerId: customerId,
			deleteProduct: map
	};
	//向接口发送删除请求
	$.ajax({ 
		url: "http://cart.spider.com.cn/cart/update",
		type: 'POST',
		contentType: 'application/json',
		dataType:"json",                 
        data:JSON.stringify(datainfo), 
		success: function(data){
			var ordercartcount = Number(allcount) - Number(quantity);
     		$("#rem" + uuid).remove();
     		$("#_ordercartnum").html("购物车里有" + ordercartcount + "件商品");
     		$("#realordercart_num").html(ordercartcount);
		},
		error: function(data){
			alert("系统繁忙");
			return false;
		}
	});
}

//商品搜索
function makeSearchUrl() {
	var o = document.formkeyword;
	var keys = o.keyword.value;
	if (keys == "" || keys == "请输入关键字 (支持中文/简拼/全拼搜索)") {
		o.keyword.focus();
		return false;
	} else {
		document.formkeyword.submit();
	}
}